package cz.hanis.android.geography.activities;

import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestManager;
import cz.hanis.android.geography.TestSpecification;
import cz.hanis.android.geography.adapters.ContinentSpinnerAdapter;
import cz.hanis.android.geography.adapters.TestSpinnerAdapter;
import cz.hanis.android.geography.adapters.TestTypeSpinnerAdapter;
import cz.hanis.android.geography.models.Continent;
import cz.hanis.android.geography.models.Test;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class TestChooserActivity extends SherlockActivity {
	
	private Spinner mContinentSpinner;
	private Spinner mTestTypeSpinner;
	private Spinner mTestSpinner;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_chooser);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		String[] continentNameArray = getResources().getStringArray(R.array.continent_array);
		int icons[] = { R.drawable.continent_europe,
				R.drawable.continent_asia, R.drawable.continent_south_america,
				R.drawable.continent_north_america, R.drawable.continent_africa,
				R.drawable.continent_oceania, R.drawable.continent_earth };
						
	    mContinentSpinner = (Spinner) findViewById(R.id.sp_test_chooser_continent);
		mContinentSpinner.setAdapter(new ContinentSpinnerAdapter(this, R.layout.continent_row, continentNameArray, icons));
		
		String[] testTypeNameArray = getResources().getStringArray(R.array.test_type_name_array);
		String[] testTypeDescriptionArray = getResources().getStringArray(R.array.test_type_description_array);
		int[] testTypeIcons = { R.drawable.test_type_life, R.drawable.test_type_time };		
		mTestTypeSpinner = (Spinner) findViewById(R.id.sp_test_chooser_type);
		mTestTypeSpinner.setAdapter(new TestTypeSpinnerAdapter(this, R.layout.test_type_row, testTypeNameArray, testTypeDescriptionArray, testTypeIcons));

		List<Test> tests = GeographyApplication.getInstance().getTestManager().getTests();
		mTestSpinner = (Spinner) findViewById(R.id.sp_test_chooser);
		TestSpinnerAdapter testSpinnerAdapter = new TestSpinnerAdapter(this, R.layout.test_row, tests);
		mTestSpinner.setAdapter(testSpinnerAdapter);
		Button btStart = (Button) findViewById(R.id.bt_test_chooser_start);		
		btStart.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				takeTest();				
			}
		});
			
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent parentActivityIntent = new Intent(this, WelcomeActivity.class);
                parentActivityIntent.addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(parentActivityIntent);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    } 		
	
	private void takeTest() {		
		//SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		//int numberOfQuestion = Integer.valueOf(preferences.getString("pref_questions_per_round", "10"));				
		//test.setNumberOfQuestion(numberOfQuestion);						
		//Intent intent = new Intent(this, FlagCountryTestActivity.class);
		TestSpecification testSpec = new TestSpecification();		
		
		int testPos = mTestSpinner.getSelectedItemPosition();
		int testTypePos = mTestTypeSpinner.getSelectedItemPosition();
		int continentPos = mContinentSpinner.getSelectedItemPosition();
		TestManager testManager = GeographyApplication.getInstance().getTestManager();		
		Test test = testManager.getTests().get(testPos);		
	    testSpec.setContinent(Continent.getIdByPosition(continentPos));
	    testSpec.setTestTypeId(testTypePos);
	    testSpec.setTestId(test.getId());
		String testClass = test.getClassName();
		if(testSpec.getTestTypeId() == TestManager.TEST_TYPE_TIME) {
			testClass += "TIME";
		}
	    Intent intent = new Intent(testClass);		
	    intent.putExtra(TestSpecification.KEY, testSpec);				
		startActivity(intent);			
	}
	
}
